//
//  DeviceSNTableViewCell.swift
//  kxf
//
//  Created by LNS2 on 2024/4/21.
//

import Foundation
import UIKit

class DeviceSNTableViewCell: UITableViewCell {
    
    var updateBlock:(()->())?
    var bindBlock:(()->())?
    var showBlock:(()->())?
    var showSignCodeBlock:(()->())?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        initUI()
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.isUserInteractionEnabled = true
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        vi.backgroundColor = .white
        
        return vi
    }()
    lazy var statusBgView : UIView = {
        let vi = UIView()
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        vi.backgroundColor = .THEME
        
        return vi
    }()
    lazy var statusLabel : UILabel = {
        let lab = UILabel()
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.textColor = .white
        lab.text = "已关联"
        lab.textAlignment = .center
        
        return lab
    }()
    lazy var nameLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .regular)
        return lab
    }()
    lazy var typeLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        return lab
    }()
    lazy var snNoLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.numberOfLines = 0
        lab.lineBreakMode = .byWordWrapping
        return lab
    }()
    lazy var voiceNoLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        return lab
    }()
    lazy var deviceIdLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        return lab
    }()
    lazy var deviceStatusLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        return lab
    }()
    lazy var deviceRemarkLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        return lab
    }()
    lazy var deviceFailMsgLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        return lab
    }()
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        return vi
    }()
    lazy var updateNameButton : UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = .white
        btn.layer.borderWidth = kFitWidth(1)
        btn.layer.borderColor = UIColor.THEME.cgColor
        btn.setTitleColor(.THEME, for: .normal)
        btn.setTitle("修改设备名称", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        
        btn.addTarget(self, action: #selector(updateAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var unBindButton : UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = .white
        btn.layer.borderWidth = kFitWidth(1)
        btn.layer.borderColor = UIColor.THEME.cgColor
        btn.setTitleColor(.THEME, for: .normal)
        btn.setTitle("解绑音响", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.addTarget(self, action: #selector(bindAction), for: .touchUpInside)
        return btn
    }()
    lazy var showQrcodeButton : UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = .white
        btn.layer.borderWidth = kFitWidth(1)
        btn.layer.borderColor = UIColor.THEME.cgColor
        btn.setTitleColor(.THEME, for: .normal)
        btn.setTitle("查看收款码", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.addTarget(self, action: #selector(showAction), for: .touchUpInside)
        return btn
    }()
    lazy var signQrcodeButton : UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.isHidden = true
        btn.backgroundColor = .white
        btn.layer.borderWidth = kFitWidth(1)
        btn.layer.borderColor = UIColor.THEME.cgColor
        btn.setTitleColor(.THEME, for: .normal)
        btn.setTitle("签约二维码", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.addTarget(self, action: #selector(showSignCodeAction), for: .touchUpInside)
        return btn
    }()
    
}

extension DeviceSNTableViewCell{
    func updateUI(dict:NSDictionary) {
        nameLabel.text = dict["posName"]as? String ?? ""
        typeLabel.text = "设备类型：\(dict["posModel"]as? String ?? "")"
        snNoLabel.text = "SN：\(dict["posNumber"]as? String ?? "")"
        
        if dict["izRelation"]as? String ?? "" == "Y"{
            statusBgView.backgroundColor = .THEME
            statusLabel.text = "已关联"
            unBindButton.setTitle("解绑音响", for: .normal)
            voiceNoLabel.text = "关联音响SN：\(dict["bindPosNumber"]as? String ?? "")"
        }else{
            statusBgView.backgroundColor = WHColor_16(colorStr: "C4C4C4")
            statusLabel.text = "未关联"
            unBindButton.setTitle("关联音响", for: .normal)
            voiceNoLabel.text = ""
        }
    }
    func updateUIForVoice(dict:NSDictionary) {
        nameLabel.text = dict["posName"]as? String ?? ""
        typeLabel.text = "设备类型：\(dict["posModel"]as? String ?? "")"
        snNoLabel.text = "SN：\(dict["posNumber"]as? String ?? "")"
        unBindButton.isHidden = true
//        showQrcodeButton.isHidden = true
        statusLabel.isHidden = true
        statusBgView.isHidden = true
        
        updateNameButton.snp.remakeConstraints { make in
            make.centerY.lessThanOrEqualTo(showQrcodeButton)
            make.right.equalTo(showQrcodeButton.snp.left).offset(kFitWidth(-9))
            make.width.height.equalTo(showQrcodeButton)
        }
        if dict["posModel"]as? String ?? "" == "N7HD"{
            deviceIdLabel.text = "设备编号：\(dict["deviceId"]as? String ?? "")"
            if (dict["orderNo"]as? String ?? "").count > 0 && dict["orderNo"]as? String ?? "" != "null" && dict["orderNo"]as? String ?? "" != "<null>"{
                deviceStatusLabel.text = "设备申请状态：\(dict["applyStatusName"]as? String ?? "")"
                signQrcodeButton.isHidden = false
                showQrcodeButton.snp.remakeConstraints { make in
                    make.right.equalTo(signQrcodeButton.snp.left).offset(kFitWidth(-9))
                    make.bottom.equalTo(kFitWidth(-11))
                    make.width.equalTo(kFitWidth(80))
                    make.height.equalTo(kFitWidth(24))
                }
                if (dict["deviceRemark"]as? String ?? "").count > 0 {
                    deviceRemarkLabel.text = "设备绑定备注：\(dict["deviceRemark"]as? String ?? "")"
                }
                if (dict["errorCode"]as? String ?? "").count > 0 {
                    deviceFailMsgLabel.text = "申请失败原因：\(dict["errorCode"]as? String ?? "")"
                }
            }
        }
    }
    func updateUIForVoiceBind(dict:NSDictionary) {
        nameLabel.text = dict["posName"]as? String ?? ""
        typeLabel.text = "设备类型：\(dict["posModel"]as? String ?? "")"
        snNoLabel.text = "SN：\(dict["posNumber"]as? String ?? "")"
        unBindButton.isHidden = true
        showQrcodeButton.isHidden = true
        statusLabel.isHidden = true
        statusBgView.isHidden = true
        updateNameButton.isHidden = true
        lineView.isHidden = true
    }
    @objc func updateAction() {
        if self.updateBlock != nil{
            self.updateBlock!()
        }
    }
    
    @objc func bindAction(){
        if self.bindBlock != nil{
            self.bindBlock!()
        }
    }
    @objc func showAction(){
        if self.showBlock != nil{
            self.showBlock!()
        }
    }
    @objc func showSignCodeAction(){
        if self.showSignCodeBlock != nil{
            self.showSignCodeBlock!()
        }
    }
}

extension DeviceSNTableViewCell{
    func initUI() {
        contentView.addSubview(whiteView)
        whiteView.addSubview(nameLabel)
        whiteView.addSubview(typeLabel)
        whiteView.addSubview(snNoLabel)
        whiteView.addSubview(voiceNoLabel)
        whiteView.addSubview(deviceIdLabel)
        whiteView.addSubview(deviceStatusLabel)
        whiteView.addSubview(deviceRemarkLabel)
        whiteView.addSubview(deviceFailMsgLabel)
        whiteView.addSubview(statusBgView)
        whiteView.addSubview(statusLabel)
        whiteView.addSubview(lineView)
        whiteView.addSubview(updateNameButton)
        whiteView.addSubview(unBindButton)
        whiteView.addSubview(showQrcodeButton)
        whiteView.addSubview(signQrcodeButton)
        
        setConstrait()
    }
    func setConstrait() {
        whiteView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(343))
            make.top.equalTo(kFitWidth(10))
            make.bottom.equalToSuperview()
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(15))
            make.top.equalTo(kFitWidth(16))
        }
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(14))
            make.top.equalTo(kFitWidth(43))
        }
        snNoLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(14))
            make.top.equalTo(kFitWidth(70))
        }
        deviceIdLabel.snp.makeConstraints { make in
            make.top.equalTo(snNoLabel.snp.bottom).offset(kFitWidth(14))
            make.left.equalTo(kFitWidth(14))
        }
        deviceStatusLabel.snp.makeConstraints { make in
            make.top.equalTo(deviceIdLabel.snp.bottom).offset(kFitWidth(14))
            make.left.equalTo(deviceIdLabel)
        }
        deviceRemarkLabel.snp.makeConstraints { make in
            make.top.equalTo(deviceStatusLabel.snp.bottom).offset(kFitWidth(14))
            make.left.equalTo(deviceIdLabel)
        }
        deviceFailMsgLabel.snp.makeConstraints { make in
            make.top.equalTo(deviceRemarkLabel.snp.bottom).offset(kFitWidth(14))
            make.left.equalTo(deviceIdLabel)
        }
        voiceNoLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(14))
            make.top.equalTo(snNoLabel.snp.bottom).offset(kFitWidth(14))
        }
        lineView.snp.makeConstraints { make in
//            make.top.equalTo(deviceIdLabel.snp.bottom)
            make.left.width.equalToSuperview()
            make.height.equalTo(kFitWidth(1))
            make.bottom.equalTo(kFitWidth(-46))
        }
        signQrcodeButton.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-12))
            make.bottom.equalTo(kFitWidth(-11))
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(24))
        }
        showQrcodeButton.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-12))
//            make.right.equalTo(signQrcodeButton.snp.left).offset(kFitWidth(-9))
            make.bottom.equalTo(kFitWidth(-11))
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(24))
        }
        unBindButton.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualTo(showQrcodeButton)
            make.right.equalTo(showQrcodeButton.snp.left).offset(kFitWidth(-9))
            make.width.height.equalTo(showQrcodeButton)
        }
        updateNameButton.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualTo(showQrcodeButton)
            make.right.equalTo(unBindButton.snp.left).offset(kFitWidth(-9))
            make.width.height.equalTo(showQrcodeButton)
        }
        statusBgView.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(8))
            make.top.equalTo(kFitWidth(-8))
            make.height.equalTo(kFitWidth(30))
            make.width.equalTo(kFitWidth(88))
        }
        statusLabel.snp.makeConstraints { make in
            make.right.top.equalToSuperview()
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(22))
        }
    }
}
